package com.rimi.project.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rimi.project.util.DESUtil;

@WebServlet("/user/exit")
public class ExitServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		HttpSession session = req.getSession();
		
		session.invalidate();
		
		// 清理cookie
		Cookie[] cookies = req.getCookies();
		String userName = "";
		String password = "";	
		for (Cookie cookie : cookies) {
			// 清理哪一些cookie
			// 删除cookie时，domain和path必须和原来设定的初始值保持一致
			cookie.setPath(req.getContextPath());
//			if ("username".equals(cookie.getName()) || "password".equals(cookie.getName())) {
//				// 设置缓冲时间为0，可以强制删除用户本地浏览器的cookie
//				cookie.setMaxAge(0);
//				resp.addCookie(cookie);			
//			}
			if ("token".equals(cookie.getName())) {
				cookie.setMaxAge(0);
				resp.addCookie(cookie);
			}
		}	
		req.getRequestDispatcher("/login.jsp").forward(req, resp);
	}
}
